<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Store extends CI_Controller {
	var $eData = '';
	function __construct() {
		parent::__construct ();
		$this->thor->loginstate ();
	}
	
	function index() {
		$this->load->library('pagination');
		
		$eSearch = trim($this->input->get('name'));
		$ePage = (int)$this->input->get('num');
		
		
		if($eSearch != '') 
		$this->db->like('name', $eSearch); 
		$eConfig['total_rows'] = $this->db->count_all_results('store');
		
		$this->db->order_by('createTime', 'DESC');
		$this->db->order_by('id', 'DESC');
		if($eSearch != '') 
		$this->db->like('name', $eSearch);
		$this->eData['storeList'] = $this->db->get('store', 25, $ePage)->result_array();

		$eConfig['per_page'] = 25;
		$eConfig['num_links'] = 5;
		$eConfig['base_url'] = '?1=1';
		$this->pagination->initialize($eConfig); 
		
		$this->eData['eSearch']      = $eSearch;
		$this->eData['eCount']       = $eConfig['total_rows'];
		
		$this->load->view ( 'mang/store',$this->eData );
	}
	
	function edit(){
		$eData = array();
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="eError">', '</div>');
		$id = $this->input->get('id');
		$this->db->where('id',$id);
		$eData['store'] = $this->db->get('store')->row_array();
		
		$this->load->model ( 'codevalue_model' );
		$eData ['ycpp'] = $this->codevalue_model->getCodeValueList ( 'ycpp' );
		$eData ['tsqyList'] = $this->codevalue_model->getCodeValueList ( 'tsqy' );
		$eData ['shenghuoList'] = $this->codevalue_model->getCodeValueList ( 'shenghuo' );
		$eData ['storeperson'] = $this->codevalue_model->getCodeValueList ( 'storeperson' );
		$eData ['storespebus'] = $this->codevalue_model->getCodeValueList ( 'storespebus' );
		
		$this->load->view('mang/storeedit',$eData);
	}
	
	function action(){
			$eData = array ();
			$eData['id'] = $this->input->post('id');
			$this->load->library ( 'form_validation' );
			$this->form_validation->set_error_delimiters ( '', '' );
			$this->form_validation->set_rules ( 'store_name', '小店名称', 'trim|required|max_length[300]|xss_clean' );
			$this->form_validation->set_rules ( 'store_id', '所在街道编号', 'trim|required|max_length[32]|xss_clean' );
			$this->form_validation->set_rules ( 'store_url', '小店地址', 'trim|required|xss_clean' );
			if ($this->form_validation->run () == TRUE) {
				
					
					$eData ['name'] = $this->input->post ( 'store_name' ); //小店名称
					$eData ['street'] = $this->input->post ( 'store_id' ); //英文名称
					$eData ['address'] = $this->input->post ( 'store_url' ); //小店地址
					$eData ['eaddress'] = $this->input->post ( 'eaddress' ); //英文地址
					$eData ['storetype'] = $this->input->post ( 'storetype' ); //小店分类
					$eData ['storetypes'] = $this->input->post ( 'storetypes' ); //小店分类的子分类
					$eData ['storebrand'] = $this->input->post ( 'storebrand' ); //原创品牌
					$eData ['storeperson'] = $this->input->post ( 'storeperson' ); //名人开店
					$eData ['storespebus'] = $this->input->post ( 'storespebus' ); //特色商圈
					$eData ['storespebuss'] = $this->input->post ( 'storespebuss' ); //特色商圈子类
					$eData ['style'] = $this->input->post ( 'style' ); //小店风格
					$eData ['bustime'] = $this->input->post ( 'bustime' ); //营业时间
					$eData ['phone'] = $this->input->post ( 'phone' ); //联系电话
					$eData ['history'] = $this->input->post ( 'history' ); //小店文化
					$eData ['tags'] = $this->input->post ( 'tags' ); //标签
					$eData ['storespebuss'] = $this->input->post ( 'storespebuss' ); //特色商圈子类
					
					$this->db->where("id",$eData['id']);
					$this->db->update ( 'store', $eData );
					
					redirect ( "mang/store");
			
			} else {
				redirect( 'mang/store/edit?id='.$eData['id']);
			}
		
	}
	
	function delete(){
		$eCID   = $this->input->get('cid');
		$eID    = (int)$this->input->get('id');
		if($eID == 0) $eCheckID = $this->input->post('checkID');
		else $eCheckID[] = $eID;
		
		$this->db->where_in('id', $eCheckID);
		$this->db->delete('codevalue');
		
		
		redirect('mang/codevalue/edit?id='.$eCID);
	//	echo 'mang/codevalue/edit?id='.$eCID;
	}
	
	function order(){
		$eCID   = $this->input->get('cid');
		$eCheckID = $this->input->post('checkID');
		$eOrderID = $this->input->post('orderID');

		foreach($eCheckID as $i => $item){
			$this->db->where('id', $item);
			$this->db->update('codevalue', array('sort' => $eOrderID[$i]));
		}
		
		redirect('mang/codevalue/edit?id='.$eCID);
	}
	
	function getLabel($value){
		if($value == 'shenghuo'){
			return '生活';
		}else if($value == 'fushi'){
			return '服饰';
		}else if($value == 'wenhua'){
			return '文化';
		}else if($value == 'xiuxian'){
			return '休闲';
		}else if($value == 'meishi'){
			return '美食';
		}else if($value == 'ycpp'){
			return '原创品牌';
		}else if($value == 'yishu'){
			return '艺术中心';
		}else if($value == 'ditie'){
			return '地铁小店';
		}else if($value == 'qstTip'){
			return '密码问题提示';
		}else if($value == 'tsqy'){
			return '特色区域';
		}
	}

}